package kr.co.exight.onApp;

import java.net.URL;
import java.util.ArrayList;

import library.ActionClass;
import library.JsonParser;
import library.Library;
import library.Library.DialogChoiceYes;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ActivityPreferNotice extends ListActivity {

	URL url;
	JSONObject result;
	
	NoticeAdapter adapter;
	ListView lstView;
	@Override

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_noticelist);
		
		final TextView tvEmpty = (TextView)findViewById(R.id.notice_tvEmpty);
		TextView tvName = (TextView)findViewById(R.id.notice_tvName);
		
		Library.setFontNanumGothic(tvName);
		Library.setFontNanumGothic(tvEmpty);
		lstView = getListView();
		
		showDialog(0);
		JsonParser json = new JsonParser("http://yonseihouse.com/notice/list/", new ActionClass(this) {
			@Override
			public void actionFailed() {
				// TODO Auto-generated method stub
				Library.makeYesDialog(ActivityPreferNotice.this, "인터넷 연결이 좋지 않습니다.\n연결을 확인해주세요", "확인", new DialogChoiceYes() {

					public boolean choiceYes() {
						// TODO Auto-generated method stub
						return false;
					}
				});

			}


			@Override
			public void actionSuccess(Object object) {
				// TODO Auto-generated method stub
				try{
					dismissDialog(0);
				}catch(IllegalArgumentException e){
					e.printStackTrace();
				}

				result  = (JSONObject)object;
				
				try {
					if(result.getInt("counts") != 0) {
						tvEmpty.setVisibility(View.GONE);
						
						lstView.setVisibility(View.VISIBLE);
						
						adapter = new NoticeAdapter();
						lstView.setAdapter(adapter);
	
					}else{
						tvEmpty.setVisibility(View.VISIBLE);
						lstView.setVisibility(View.GONE);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}

		});
		json.start();

	}

	@Override // 로딩바 안보이게 하기
	protected Dialog onCreateDialog(int id) {
		final Dialog dialog = new Dialog(this, R.style.Theme_Dialog);
		View view = getLayoutInflater().inflate(R.layout.dialog_progress, null);
		dialog.setContentView(view);

		dialog.setOnCancelListener(new OnCancelListener() {

			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				finish();
			}

		});
		return dialog;

	}

	private class NoticeAdapter extends BaseAdapter{
		

		public View getView(final int position, View convertView, ViewGroup parent) {
			View v = convertView;

			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.cell_noticelist, null);
			}
			
			TextView tvTitle = (TextView)v.findViewById(R.id.settingNotice_tvTitle);
			TextView tvDate = (TextView)v.findViewById(R.id.settingNotice_tvDate);
			Library.setFontNanumGothic(tvTitle);
			Library.setFontNanumGothic(tvDate);
			

			final JSONObject a;
			try {
				a = result.getJSONArray("notices").getJSONObject(position);
				
				tvTitle.setText(a.getString("title"));
				String date = a.getString("time");
				tvDate.setText(date.substring(5, 10));
				
				v.setTag(a.getString("id"));
				
			}catch(JSONException e){
				e.printStackTrace();
			}
			return v;
		}



		public int getCount() {
			// TODO Auto-generated method stub
			try {
				return result.getJSONArray("notices").length();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
		}



		public Object getItem(int position) {
			// TODO Auto-generated method stub
			try {
				return result.getJSONArray("notices").get(position);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return position;
		}
		


		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		FrameLayout f = (FrameLayout) v.findViewById(R.id.settingNotice_flContents);
		ImageView ivArrow = (ImageView)v.findViewById(R.id.settingNotice_arrow);
		TextView tvTitle = (TextView)v.findViewById(R.id.settingNotice_tvTitle);
		final TextView tvContents = (TextView)v.findViewById(R.id.settingNotice_tvContents);
		final ProgressBar pb = (ProgressBar)v.findViewById(R.id.settingNotice_pb);
		
		Library.setFontNanumGothic(tvContents);

		if (f.getVisibility() == 8) {
			tvTitle.setText(Library.setStringColor(tvTitle.getText().toString(), "red"));			
			ivArrow.setBackgroundResource(R.drawable.arrow_rred);
			f.setVisibility(View.VISIBLE);
			
		} else if (f.getVisibility() == 0) {
			tvTitle.setText(tvTitle.getText().toString());
			ivArrow.setBackgroundResource(R.drawable.arrow_grey);
			f.setVisibility(View.GONE);
		}
		
		pb.setVisibility(View.VISIBLE);
		
		String iden = (String)v.getTag();
		
		JsonParser json = new JsonParser("http://yonseihouse.com/notice/" + iden,  new ActionClass(ActivityPreferNotice.this) {
			@Override
			public void actionFailed() {
				// TODO Auto-generated method stub
				Library.makeYesDialog(ActivityPreferNotice.this, "인터넷 연결이 좋지 않습니다.\n연결을 확인해주세요", "확인", new DialogChoiceYes() {

					public boolean choiceYes() {
						// TODO Auto-generated method stub
						return false;
					}
				});
			}
			
			@Override
			public void actionSuccess(Object object) {
				// TODO Auto-generated method stub
				pb.setVisibility(View.GONE);
				try {
					tvContents.setText(((JSONObject)object).getString("body"));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		json.start();
		super.onListItemClick(l, v, position, id);
	}
}

